

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/2.jpg">
  <link rel="icon" type="image/png" href="/img/2.jpg">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="">
  <meta name="author" content="K">
  <meta name="keywords" content="">
  <title>docker-swarm集群 - K</title>

  <link  rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.staticfile.org/github-markdown-css/4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.staticfile.org/highlight.js/10.0.0/styles/github-gist.min.css" />
    
  

  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.2.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>かい</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" href="javascript:">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner intro-2" id="background" parallax=true
         style="background: url('/img/1.jpg') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="container page-header text-center fade-in-up">
            <span class="h2" id="subtitle">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2020-12-17 14:00" pubdate>
        2020年12月17日 下午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      4.1k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      62
       分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid">
  <div class="row">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-md">
      <div class="container nopadding-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto" id="post">
            <!-- SEO header -->
            <h1 style="display: none">docker-swarm集群</h1>
            
            <div class="markdown-body" id="post-body">
              <h2 id="集群"><a href="#集群" class="headerlink" title="集群"></a>集群</h2><ul>
<li><p>从主机层面来看 docker Swarm管理的是 docker_host集群 这个是一个重要概念 集群化（Clustring）</p>
</li>
<li><p>集群是由一组互相连接的服务器组成，各个服务协同工作</p>
</li>
<li><p>集群和多服务的区别:</p>
<ul>
<li>集群能像单个系统那样工作，同时提供负载均衡-高可用等功能</li>
</ul>
</li>
<li><p>使用集群部署应用时，只考虑需要多少内存和CPU，而不是考虑会使用哪台的CPU和内存。我们不应该关心应用会被部署在哪里，我们关心的是运行这个应用需要哪些资源，然后将它部署到集群，集群管理程序（比如Docker Swarm）会帮你搞定这些细节。</p>
</li>
</ul>
<h2 id="docker-swarm-mode"><a href="#docker-swarm-mode" class="headerlink" title="docker swarm mode"></a>docker swarm mode</h2><ul>
<li><p>从 v1.12 开始，Docker Swarm 的功能已经完全与 Docker Engine 集成，要管理集群，只需要启动Swarm Mode。安装好 Docker ，Swarm和服务发现就已经存在，服务发现也在那里了，而且不需要安装Consul等外部数据库。</p>
</li>
<li><p>相比Kubernetes ，用Docker Swarm创建集群非常简单，不需要额外安装任何软件，也不需要做任何额外的配置</p>
</li>
</ul>
<h2 id="Swarm集群"><a href="#Swarm集群" class="headerlink" title="Swarm集群"></a>Swarm集群</h2><ul>
<li>swarm是运行在docker Engine的多个主机组成的集群</li>
<li>当docker Engine初始化一个swarm 或者加入到一个存在的swarm时，就会启动swarm mode</li>
<li>没启动swarm mode时 docker执行的是容器的命令</li>
<li>运行swarm mode 后 docker增加了编排service的能力</li>
<li>docker允许 在同一个docker_host上运行 swarm service的同时单独运行容器</li>
</ul>
<h2 id="swarm节点"><a href="#swarm节点" class="headerlink" title="swarm节点"></a>swarm节点</h2><ul>
<li><p>swarm中的每个 docker engine都是一个node(每个dockerhost主机都是一个node节点)</p>
</li>
<li><p>分为 manager 和 worker</p>
</li>
<li><p>docker_node 执行部署命令 manager就会将部署任务拆解并分配给一个或多个 worker 完成部署</p>
</li>
<li><p>manager node负责执行编排和管理集群 保持并维护swarm处于期望的状态 (期望值是表示在swarm集群中永远容器的数量保持这个值不变)</p>
</li>
<li><p>worker node 接受并执行由manager node 派发的任务。默认配置下manager node 同时也是一个 worker node，不过可以将其配置成manager-only mode，让其专职负责编排和集群管理工作。</p>
</li>
<li><p>worker node 会定期向manager node 报告自己的状态和他正在执行的任务状态，这样，manager 就可以维护整个集群的状态</p>
</li>
</ul>
<h2 id="swarm-状态"><a href="#swarm-状态" class="headerlink" title="swarm 状态"></a>swarm 状态</h2><ul>
<li>分为 leader 和 reachable 状态</li>
<li>当leader宕机后，会有一个 reachable 被选为 leader继续进行管理集群的工作</li>
</ul>
<h2 id="docker-service"><a href="#docker-service" class="headerlink" title="docker service"></a>docker service</h2><ul>
<li><p>service定义了 worker node上要执行的任务，swarm的主要编排任务就是要保证service处于期望状态下(最少运行多少容器)</p>
<blockquote>
<p>如：在swarm 中启动一个http访问，使用的镜像是httpd:lastes，副本数是3</p>
</blockquote>
<blockquote>
<p>manager node 负责创建这个service ，经过分析指导需要启动3个httpd 容器，根据当前各worker node 的状态将运行容器的任务分配下去，比如worker1 上运行两个容器，worker2上运行一个容器。</p>
</blockquote>
<blockquote>
<p>运行了一段时间，worker2 突然宕机了，manager 监控到这个故障，于是立即在worker3 上启动了一个新的httpd容器。<br>这样就保证了 service 处于期望的三个副本状态。</p>
</blockquote>
</li>
</ul>
<h2 id="swarm-任务状态"><a href="#swarm-任务状态" class="headerlink" title="swarm 任务状态"></a>swarm 任务状态</h2><table>
<thead>
<tr>
<th>状态</th>
<th>解释</th>
</tr>
</thead>
<tbody><tr>
<td>NEW</td>
<td>任务初始化</td>
</tr>
<tr>
<td>PENDING</td>
<td>已分配任务资源</td>
</tr>
<tr>
<td>ASSIGNED</td>
<td>docker将任务分配给节点</td>
</tr>
<tr>
<td>ACCEPTED</td>
<td>任务已被工作节点接收，如果辅助几点拒绝任务，则状态改为REJECTED</td>
</tr>
<tr>
<td>PREPARING</td>
<td>docker正在启动任务</td>
</tr>
<tr>
<td>RUNNING</td>
<td>任务正在执行</td>
</tr>
<tr>
<td>COMPLETE</td>
<td>任务退出 没有错误代码</td>
</tr>
<tr>
<td>FAILED</td>
<td>任务退出并显示错误代码</td>
</tr>
<tr>
<td>SHUTDOWN</td>
<td>docker请求关闭任务</td>
</tr>
<tr>
<td>REJECTED</td>
<td>工作节点拒绝了任务</td>
</tr>
<tr>
<td>ORPHANED</td>
<td>节点关闭时间过长</td>
</tr>
<tr>
<td>REMOVE</td>
<td>该任务不是终端，但关联的服务已被删除或者按比例缩小</td>
</tr>
</tbody></table>
<h2 id="swarm环境搭建"><a href="#swarm环境搭建" class="headerlink" title="swarm环境搭建"></a>swarm环境搭建</h2><table>
<thead>
<tr>
<th>主机</th>
<th>服务</th>
</tr>
</thead>
<tbody><tr>
<td>192.168.100.211</td>
<td>docker</td>
</tr>
<tr>
<td>192.168.100.212</td>
<td>docker</td>
</tr>
<tr>
<td>192.168.100.213</td>
<td>docker</td>
</tr>
</tbody></table>
<ul>
<li><p>注意：docker版本必须一致</p>
</li>
<li><p>最好在manager节点搭建一个私有仓库，要不然下载的镜像可能不一致</p>
<blockquote>
<p>所有主机 </p>
</blockquote>
<pre><code class="hljs angelscript"><span class="hljs-string">[root@localhost ~]</span># vim /etc/hosts
<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>   localhost localhost.localdomain localhost4 localhost4.localdomain4
::<span class="hljs-number">1</span>         localhost localhost.localdomain localhost6 localhost6.localdomain6
<span class="hljs-number">192.168</span><span class="hljs-number">.100</span><span class="hljs-number">.211</span> node1
<span class="hljs-number">192.168</span><span class="hljs-number">.100</span><span class="hljs-number">.212</span> node2
<span class="hljs-number">192.168</span><span class="hljs-number">.100</span><span class="hljs-number">.213</span> node3

<span class="hljs-string">[root@node1 ~]</span>#  hostname node1 # <span class="hljs-number">211</span>
<span class="hljs-string">[root@node2 ~]</span>#  hostname node2 # <span class="hljs-number">212</span>
<span class="hljs-string">[root@node3 ~]</span>#  hostname node3 # <span class="hljs-number">213</span>

<span class="hljs-string">[root@node1 ~]</span>#  iptables -F
<span class="hljs-string">[root@node2 ~]</span>#  iptables -F
<span class="hljs-string">[root@node3 ~]</span>#  iptables -F</code></pre>
<blockquote>
<p>192.168.100.211对两台主机做免密登录</p>
</blockquote>
<pre><code class="hljs angelscript"><span class="hljs-string">[root@node1 ~]</span>#  ssh-keygen
<span class="hljs-string">[root@node1 ~]</span>#  ssh-copy-id -i <span class="hljs-number">192.168</span><span class="hljs-number">.100</span><span class="hljs-number">.212</span>
<span class="hljs-string">[root@node1 ~]</span>#  ssh-copy-id -i <span class="hljs-number">192.168</span><span class="hljs-number">.100</span><span class="hljs-number">.213</span>
</code></pre>
<blockquote>
<p>192.168.100.211启动swarm</p>
</blockquote>
<pre><code class="hljs angelscript"><span class="hljs-string">[root@node1 ~]</span>#  docker swarm  init --advertise-addr <span class="hljs-number">192.168</span><span class="hljs-number">.100</span><span class="hljs-number">.211</span>
Swarm initialized: current node (wtr5cwixf5yj834p81nqbd4ru) <span class="hljs-keyword">is</span> now a manager.

To add a worker to <span class="hljs-keyword">this</span> swarm, run the following command:

    # 以worker身份加入集群用下面的命令
    docker swarm join --token SWMTKN<span class="hljs-number">-1</span><span class="hljs-number">-51</span>c0amrai5ephbnhsnmrah00ni9iko2zvloldxdcln4ahp6swu<span class="hljs-number">-68f</span>0mv49it7njsd942rdagtf1 <span class="hljs-number">192.168</span><span class="hljs-number">.100</span><span class="hljs-number">.211</span>:<span class="hljs-number">2377</span>
 # 以 manager身份加入集群用下面的命令
 # 不过manager 不能以双数运行 只能以单数运行，最多<span class="hljs-number">7</span>个manager 不能少于 (n<span class="hljs-number">-1</span>) /<span class="hljs-number">2</span> 个
To add a manager to <span class="hljs-keyword">this</span> swarm, run <span class="hljs-string">&#x27;docker swarm join-token manager&#x27;</span> <span class="hljs-keyword">and</span> follow the instructions.</code></pre>
<blockquote>
<p>192.168.100.212以worker的身份加入集群</p>
</blockquote>
<pre><code class="hljs angelscript"><span class="hljs-string">[root@node2 ~]</span># docker swarm join --token SWMTKN<span class="hljs-number">-1</span><span class="hljs-number">-51</span>c0amrai5ephbnhsnmrah00ni9iko2zvloldxdcln4ahp6swu<span class="hljs-number">-68f</span>0mv49it7njsd942rdagtf1 <span class="hljs-number">192.168</span><span class="hljs-number">.100</span><span class="hljs-number">.211</span>:<span class="hljs-number">2377</span>
This node joined a swarm as a worker.
</code></pre>
<blockquote>
<p>192.168.100.213 以manager身份加入集群</p>
</blockquote>
<pre><code class="hljs angelscript"># node1 先查看以manager方式加入的命令
<span class="hljs-string">[root@node1 ~]</span>#  docker swarm join-token manager
To add a manager to <span class="hljs-keyword">this</span> swarm, run the following command:
   # 复制这个就可以以manager加入集群
    docker swarm join --token SWMTKN<span class="hljs-number">-1</span><span class="hljs-number">-51</span>c0amrai5ephbnhsnmrah00ni9iko2zvloldxdcln4ahp6swu<span class="hljs-number">-35</span>ais8910hm4i361kxqi67f4k <span class="hljs-number">192.168</span><span class="hljs-number">.100</span><span class="hljs-number">.211</span>:<span class="hljs-number">2377</span>

# 加入集群
<span class="hljs-string">[root@node3 ~]</span># docker swarm join --token SWMTKN<span class="hljs-number">-1</span><span class="hljs-number">-51</span>c0amrai5ephbnhsnmrah00ni9iko2zvloldxdcln4ahp6swu<span class="hljs-number">-35</span>ais8910hm4i361kxqi67f4k <span class="hljs-number">192.168</span><span class="hljs-number">.100</span><span class="hljs-number">.211</span>:<span class="hljs-number">2377</span>
This node joined a swarm as a manager.</code></pre>
<blockquote>
<p>192.168.100.211 查看</p>
</blockquote>
<pre><code class="hljs yaml">[<span class="hljs-string">root@node1</span> <span class="hljs-string">~</span>]<span class="hljs-comment">#  docker info </span>
  <span class="hljs-attr">Node Address:</span> <span class="hljs-number">192.168</span><span class="hljs-number">.100</span><span class="hljs-number">.211</span>
  <span class="hljs-attr">Manager Addresses:</span>
   <span class="hljs-number">192.168</span><span class="hljs-number">.100</span><span class="hljs-number">.211</span><span class="hljs-string">:2377</span>
   <span class="hljs-number">192.168</span><span class="hljs-number">.100</span><span class="hljs-number">.213</span><span class="hljs-string">:2377</span>
 <span class="hljs-comment"># 这个警告是manager不能以双数运行</span>
<span class="hljs-attr">WARNING:</span> <span class="hljs-string">Running</span> <span class="hljs-string">Swarm</span> <span class="hljs-string">in</span> <span class="hljs-string">a</span> <span class="hljs-string">two-manager</span> <span class="hljs-string">configuration.</span> <span class="hljs-string">This</span> <span class="hljs-string">configuration</span> <span class="hljs-string">provides</span>
         <span class="hljs-literal">no</span> <span class="hljs-string">fault</span> <span class="hljs-string">tolerance,</span> <span class="hljs-string">and</span> <span class="hljs-string">poses</span> <span class="hljs-string">a</span> <span class="hljs-string">high</span> <span class="hljs-string">risk</span> <span class="hljs-string">to</span> <span class="hljs-string">lose</span> <span class="hljs-string">control</span> <span class="hljs-string">over</span> <span class="hljs-string">the</span> <span class="hljs-string">cluster.</span>
         <span class="hljs-string">Refer</span> <span class="hljs-string">to</span> <span class="hljs-string">https://docs.docker.com/engine/swarm/admin_guide/</span> <span class="hljs-string">to</span> <span class="hljs-string">configure</span> <span class="hljs-string">the</span>
         <span class="hljs-string">Swarm</span> <span class="hljs-string">for</span> <span class="hljs-string">fault-tolerance.</span>
<span class="hljs-comment"># 查看集群</span>
[<span class="hljs-string">root@node1</span> <span class="hljs-string">~</span>]<span class="hljs-comment">#  docker node ls</span>
<span class="hljs-string">ID</span>                            <span class="hljs-string">HOSTNAME</span>            <span class="hljs-string">STATUS</span>              <span class="hljs-string">AVAILABILITY</span>        <span class="hljs-string">MANAGER</span> <span class="hljs-string">STATUS</span>      <span class="hljs-string">ENGINE</span> <span class="hljs-string">VERSION</span>
<span class="hljs-string">wtr5cwixf5yj834p81nqbd4ru</span> <span class="hljs-string">*</span>   <span class="hljs-string">node1</span>               <span class="hljs-string">Ready</span>               <span class="hljs-string">Active</span>              <span class="hljs-string">Leader</span>              <span class="hljs-number">19.03</span><span class="hljs-number">.13</span>
<span class="hljs-string">8x4c8yszder0hc4vvlvdqb7o3</span>     <span class="hljs-string">node2</span>               <span class="hljs-string">Ready</span>               <span class="hljs-string">Active</span>                                  <span class="hljs-number">19.03</span><span class="hljs-number">.13</span>
<span class="hljs-string">kaxa6st33ed2xl03x8q1l71gj</span>     <span class="hljs-string">node3</span>               <span class="hljs-string">Ready</span>               <span class="hljs-string">Active</span>              <span class="hljs-string">Reachable</span>           <span class="hljs-number">19.03</span><span class="hljs-number">.13</span>
</code></pre>

</li>
</ul>
<h2 id="manager降级为worker"><a href="#manager降级为worker" class="headerlink" title="manager降级为worker"></a>manager降级为worker</h2><p> <code>docker node demote 节点名称</code></p>
<blockquote>
<p>192.168.100.213 降级为 worker<br> (192.168.100.211执行) 就是在manager节点执行</p>
</blockquote>
<pre><code class="hljs yaml">[<span class="hljs-string">root@node1</span> <span class="hljs-string">~</span>]<span class="hljs-comment">#  docker node demote node3 </span>
<span class="hljs-string">Manager</span> <span class="hljs-string">node3</span> <span class="hljs-string">demoted</span> <span class="hljs-string">in</span> <span class="hljs-string">the</span> <span class="hljs-string">swarm.</span>

[<span class="hljs-string">root@node1</span> <span class="hljs-string">~</span>]<span class="hljs-comment">#  docker info</span>
  <span class="hljs-attr">Node Address:</span> <span class="hljs-number">192.168</span><span class="hljs-number">.100</span><span class="hljs-number">.211</span>
  <span class="hljs-attr">Manager Addresses:</span>
   <span class="hljs-number">192.168</span><span class="hljs-number">.100</span><span class="hljs-number">.211</span><span class="hljs-string">:2377</span>
</code></pre>

<h2 id="worker-升级为-manager"><a href="#worker-升级为-manager" class="headerlink" title="worker 升级为 manager"></a>worker 升级为 manager</h2><p><code>docker node promote 节点名称</code></p>
<pre><code class="hljs angelscript"><span class="hljs-string">[root@node1 ~]</span>#  docker node promote node2
Node node2 promoted to a manager <span class="hljs-keyword">in</span> the swarm.

<span class="hljs-string">[root@node1 ~]</span>#  docker info
  Node Address: <span class="hljs-number">192.168</span><span class="hljs-number">.100</span><span class="hljs-number">.211</span>
  Manager Addresses:
   <span class="hljs-number">192.168</span><span class="hljs-number">.100</span><span class="hljs-number">.211</span>:<span class="hljs-number">2377</span>
   <span class="hljs-number">192.168</span><span class="hljs-number">.100</span><span class="hljs-number">.212</span>:<span class="hljs-number">2377</span></code></pre>
<h2 id="将节点踢出集群"><a href="#将节点踢出集群" class="headerlink" title="将节点踢出集群"></a>将节点踢出集群</h2><p> <code> docker node rm 节点id \ docker swarm leave -f</code></p>
<blockquote>
<p>将192.168.100.213踢出集群</p>
</blockquote>
<pre><code class="hljs angelscript"># <span class="hljs-number">192.168</span><span class="hljs-number">.100</span><span class="hljs-number">.213</span> 退出集群
<span class="hljs-string">[root@node3 ~]</span># docker swarm leave -f
Node left the swarm.

# <span class="hljs-number">192.168</span><span class="hljs-number">.100</span><span class="hljs-number">.211</span> 删除
<span class="hljs-string">[root@node1 ~]</span>#  docker node rm node3 
node3</code></pre>
<h2 id="默认的集群管理通讯端口"><a href="#默认的集群管理通讯端口" class="headerlink" title="默认的集群管理通讯端口"></a>默认的集群管理通讯端口</h2><ul>
<li>默认的集群管理通讯端口 2377</li>
<li>TCP/UDP：7946</li>
<li>节点间通讯端口：4789</li>
</ul>
<h2 id="创建service副本"><a href="#创建service副本" class="headerlink" title="创建service副本"></a>创建service副本</h2><ul>
<li>service不能直接创建容器 但是可以包含多个容器<blockquote>
<p>创建 1个service<br><code>docker service create --name service名(也是容器名) 镜像名 </code></p>
</blockquote>
</li>
</ul>
<pre><code class="hljs routeros">[root@node1 ~]#  docker<span class="hljs-built_in"> service </span>create --name web_server httpd
9kwpu4nki6bzbtkyzffoso5sx
overall progress: 1 out of 1 tasks 
1/1: running   
verify:<span class="hljs-built_in"> Service </span>converged</code></pre>
<blockquote>
<p>查看service</p>
</blockquote>
<pre><code class="hljs routeros">[root@node1 ~]#  docker<span class="hljs-built_in"> service </span>ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
9kwpu4nki6bz        web_server          replicated          1/1                 httpd:latest     
<span class="hljs-comment"># Replicas代表这个service有几个副本处于期望状态（副本永远保持这个值不变）</span>

<span class="hljs-comment"># 查看service里有几个容器</span>
[root@node1 ~]#  docker<span class="hljs-built_in"> service </span>ps web_server 
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE           <span class="hljs-builtin-name">ERROR</span>               PORTS
eybppt6sw7ic        web_server.1        httpd:latest        node1               Running             Running 2 minutes ago    
<span class="hljs-comment"># 容器运行在 三个几点的node1中，也就是本地节点</span>
<span class="hljs-comment"># 查看一下</span>
[root@node1 ~]#  docker ps
CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS              PORTS               NAMES
66e93ff726f7        httpd:latest        <span class="hljs-string">&quot;httpd-foreground&quot;</span>   3 minutes ago       Up 3 minutes        80/tcp              web_server.1.eybppt6sw7icb4xb39pp369sy</code></pre>
<blockquote>
<p>创建多个容器的service副本<br>  <code>docker service create --name service名(也是容器名) --replicas 期望值(容器的数量) 镜像名 </code></p>
</blockquote>
<pre><code class="hljs routeros">[root@node1 ~]# docker<span class="hljs-built_in"> service </span>create --name web_server --replicas 2 httpd
kkdd8xaugx1sedqz7zsmy8fit
overall progress: 2 out of 2 tasks 
1/2: running   
2/2: running   
verify:<span class="hljs-built_in"> Service </span>converged 

<span class="hljs-comment"># 查看一下</span>
verify:<span class="hljs-built_in"> Service </span>converged 
[root@node1 ~]# docker<span class="hljs-built_in"> service </span>ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
kkdd8xaugx1s        web_server          replicated          2/2                 httpd:latest</code></pre>

<h2 id="docker-swarm-页面管理器"><a href="#docker-swarm-页面管理器" class="headerlink" title="docker swarm 页面管理器"></a>docker swarm 页面管理器</h2><p> <code> dockersamples/visualizer</code></p>
<blockquote>
<p>下载图像化工具镜像</p>
</blockquote>
<pre><code class="hljs autoit">[root<span class="hljs-symbol">@node1</span> ~]<span class="hljs-meta">#  docker pull dockersamples/visualizer</span></code></pre>
<blockquote>
<p>启动容器</p>
</blockquote>
<pre><code class="hljs angelscript"><span class="hljs-string">[root@node1 ~]</span>#  docker run -itd -p <span class="hljs-number">8080</span>:<span class="hljs-number">8080</span> -e HOST=<span class="hljs-number">192.168</span><span class="hljs-number">.100</span><span class="hljs-number">.211</span> -e PORT=<span class="hljs-number">8080</span> --volume /var/run/docker.sock:/var/run/docker.sock --name visualizer --restart always dockersamples/visualizer
b9149ccff2383a1e332dd09e86373707d7c4f6c388faf20082460ddc56d0dfbb
</code></pre>
<p> <code> 192.168.100.211:8080</code><br><img src="/images/docker/swarmtxh.png" srcset="/img/loading.gif"></p>
<h2 id="service-弹性伸缩"><a href="#service-弹性伸缩" class="headerlink" title="service 弹性伸缩"></a>service 弹性伸缩</h2><ul>
<li>把节点部署伸展为多个<blockquote>
<p>把 web_server伸展为5个</p>
</blockquote>
</li>
</ul>
<pre><code class="hljs routeros">[root@node1 ~]# docker<span class="hljs-built_in"> service </span>scale <span class="hljs-attribute">web_server</span>=5
web_server scaled <span class="hljs-keyword">to</span> 5
overall progress: 5 out of 5 tasks 
1/5: running   
2/5: running   
3/5: running   
4/5: running   
5/5: running   
verify:<span class="hljs-built_in"> Service </span>converged 

<span class="hljs-comment"># 查看service</span>
[root@node1 ~]# docker<span class="hljs-built_in"> service </span>ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
9kwpu4nki6bz        web_server          replicated          5/5                 httpd:latest    
<span class="hljs-comment"># 该service有5个副本了</span>

<span class="hljs-comment"># 查看每个每个节点运行了哪些容器</span>
[root@node1 ~]# docker<span class="hljs-built_in"> service </span>ps web_server 
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE            <span class="hljs-builtin-name">ERROR</span>               PORTS
eybppt6sw7ic        web_server.1        httpd:latest        node1               Running             Running 17 minutes ago                       
78p5ppn6xxfu        web_server.2        httpd:latest        node3               Running             Running 2 minutes ago                        
mbfwslol227h        web_server.3        httpd:latest        node3               Running             Running 2 minutes ago                        
wy3mxtsmcrto        web_server.4        httpd:latest        node1               Running             Running 2 minutes ago                        
y28opcrl5dl8        web_server.5        httpd:latest        node2               Running             Running 2 minutes ago</code></pre>
<p><img src="/images/docker/swarmsz.png" srcset="/img/loading.gif"></p>
<h2 id="manager-取消运算"><a href="#manager-取消运算" class="headerlink" title="manager 取消运算"></a>manager 取消运算</h2><ul>
<li>一般情况下，manager上不参与运算，只需要分配和调度任务—也就将模式active-&gt;drain<br><code>docker node update --availability drain 节点名称</code><pre><code class="hljs yaml"><span class="hljs-comment"># manager取消运算</span>
<span class="hljs-string">drain</span> <span class="hljs-string">下水道模式</span> <span class="hljs-string">就是不参与运算了只负责分配任务</span>
[<span class="hljs-string">root@node1</span> <span class="hljs-string">~</span>]<span class="hljs-comment"># docker node update --availability drain node1 </span>
<span class="hljs-string">node1</span>

<span class="hljs-comment"># 查看 node1 已经变成 drain了</span>
[<span class="hljs-string">root@node1</span> <span class="hljs-string">~</span>]<span class="hljs-comment"># docker node ls</span>
<span class="hljs-string">ID</span>                            <span class="hljs-string">HOSTNAME</span>            <span class="hljs-string">STATUS</span>              <span class="hljs-string">AVAILABILITY</span>        <span class="hljs-string">MANAGER</span> <span class="hljs-string">STATUS</span>      <span class="hljs-string">ENGINE</span> <span class="hljs-string">VERSION</span>
<span class="hljs-string">wtr5cwixf5yj834p81nqbd4ru</span> <span class="hljs-string">*</span>   <span class="hljs-string">node1</span>               <span class="hljs-string">Ready</span>               <span class="hljs-string">Drain</span>               <span class="hljs-string">Leader</span>              <span class="hljs-number">19.03</span><span class="hljs-number">.13</span>
<span class="hljs-string">8x4c8yszder0hc4vvlvdqb7o3</span>     <span class="hljs-string">node2</span>               <span class="hljs-string">Ready</span>               <span class="hljs-string">Active</span>                                  <span class="hljs-number">19.03</span><span class="hljs-number">.13</span>
<span class="hljs-string">oyu1h6nqhwc05h41qjopjhtz4</span>     <span class="hljs-string">node3</span>               <span class="hljs-string">Ready</span>               <span class="hljs-string">Active</span>                                  <span class="hljs-number">19.03</span><span class="hljs-number">.13</span>
</code></pre>
<ul>
<li>node1没有运行的容器了<br><img src="/images/docker/swarmdrain.png" srcset="/img/loading.gif"></li>
</ul>
</li>
</ul>
<h2 id="manager-参与运算"><a href="#manager-参与运算" class="headerlink" title="manager 参与运算"></a>manager 参与运算</h2><p>  <code>docker node update --availability active 节点名称</code></p>
<pre><code class="hljs gcode">[root@<span class="hljs-symbol">node1</span> ~]<span class="hljs-attr"># docker node update --availability active node1</span>
<span class="hljs-symbol">node1</span>

<span class="hljs-attr"># 查看</span>
<span class="hljs-attr">[root@node1</span> ~]<span class="hljs-attr"># docker node ls</span>
<span class="hljs-attr">ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION</span>
<span class="hljs-attr">wtr5</span>cwixf<span class="hljs-number">5</span>yj<span class="hljs-number">834</span>p<span class="hljs-number">81</span><span class="hljs-symbol">nqbd4</span>ru *   <span class="hljs-symbol">node1</span>               Ready               Active              Leader              <span class="hljs-number">19.03.</span><span class="hljs-number">13</span>
<span class="hljs-number">8</span>x<span class="hljs-number">4</span>c<span class="hljs-number">8</span>yszder<span class="hljs-number">0</span>hc<span class="hljs-number">4</span>vvlvdqb<span class="hljs-number">7</span><span class="hljs-meta">o3</span>     <span class="hljs-symbol">node2</span>               Ready               Active                                  <span class="hljs-number">19.03.</span><span class="hljs-number">13</span>
oyu<span class="hljs-number">1</span>h<span class="hljs-number">6</span><span class="hljs-symbol">nqhwc05</span>h<span class="hljs-number">41</span>qjopjhtz<span class="hljs-number">4</span>     <span class="hljs-symbol">node3</span>               Ready               Active                                  <span class="hljs-number">19.03.</span><span class="hljs-number">13</span></code></pre>
<ul>
<li>重新让manager参与运算容器也不会重新迁移到原来的地方<br><img src="/images/docker/swarmdrain.png" srcset="/img/loading.gif"></li>
</ul>
<h2 id="service缩减副本"><a href="#service缩减副本" class="headerlink" title="service缩减副本"></a>service缩减副本</h2><ul>
<li>和增加副本一样，只需要把副本的数量改少就可以了<pre><code class="hljs routeros"><span class="hljs-comment"># 现在有5个</span>
[root@node1 ~]# docker<span class="hljs-built_in"> service </span>ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
9kwpu4nki6bz        web_server          replicated          5/5                 httpd:latest        
<span class="hljs-comment"># 缩减为3个副本</span>
[root@node1 ~]# docker<span class="hljs-built_in"> service </span>scale <span class="hljs-attribute">web_server</span>=3
web_server scaled <span class="hljs-keyword">to</span> 3
overall progress: 3 out of 3 tasks 
1/3:   
2/3: running   
3/3: running   
verify:<span class="hljs-built_in"> Service </span>converged 
<span class="hljs-comment"># 查看一下</span>
[root@node1 ~]# docker<span class="hljs-built_in"> service </span>ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
9kwpu4nki6bz        web_server          replicated          3/3                 httpd:latest</code></pre>
<img src="/images/docker/swarmsj.png" srcset="/img/loading.gif"></li>
</ul>
<h2 id="删除-service"><a href="#删除-service" class="headerlink" title="删除 service"></a>删除 service</h2><p> <code> docker service rm service名称</code></p>
<pre><code class="hljs routeros"><span class="hljs-comment"># 删除 web-server节点</span>
[root@node1 ~]# docker<span class="hljs-built_in"> service </span>rm web_server 
web_server
[root@node1 ~]# docker<span class="hljs-built_in"> service </span>ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS

</code></pre>
<h2 id="swarm集群端口映射"><a href="#swarm集群端口映射" class="headerlink" title="swarm集群端口映射"></a>swarm集群端口映射</h2><blockquote>
<p>创建一个service副本</p>
</blockquote>
<pre><code class="hljs routeros">[root@node1 ~]# docker<span class="hljs-built_in"> service </span>create --name web_server --replicas 2 httpd
kkdd8xaugx1sedqz7zsmy8fit
overall progress: 2 out of 2 tasks 
1/2: running   
2/2: running   
verify:<span class="hljs-built_in"> Service </span>converged 

[root@node1 ~]# docker<span class="hljs-built_in"> service </span>ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
kkdd8xaugx1s        web_server          replicated          2/2                 httpd:latest</code></pre>
<blockquote>
<p>增加端口映射<br> <code>docker service update --publish-add 主机端口:容器端口 service副本名称 </code></p>
</blockquote>
<pre><code class="hljs angelscript"><span class="hljs-string">[root@node1 ~]</span># docker service update --publish-add <span class="hljs-number">80</span>:<span class="hljs-number">80</span> web_server 
web_server
overall progress: <span class="hljs-number">2</span> <span class="hljs-keyword">out</span> of <span class="hljs-number">2</span> tasks 
<span class="hljs-number">1</span>/<span class="hljs-number">2</span>: running   
<span class="hljs-number">2</span>/<span class="hljs-number">2</span>: running   
verify: Service converged 

# 访问
<span class="hljs-string">[root@node1 ~]</span># curl <span class="hljs-number">192.168</span><span class="hljs-number">.100</span><span class="hljs-number">.211</span>
&lt;html&gt;&lt;body&gt;&lt;h1&gt;It works!&lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;
</code></pre>

<h2 id="Routing-mesh"><a href="#Routing-mesh" class="headerlink" title="Routing mesh"></a>Routing mesh</h2><p><img src="/images/docker/Routingmesh.png" srcset="/img/loading.gif"></p>
<ul>
<li>计算机内部有个 swarm load balance 服务 </li>
<li>当访问到 80端口后 docker都会将所有请求发送到活动的容器上，在集群节点本身上 80端口没有被绑定，但路由网格防止发生端口冲突，会给每个节点分配一个ip 进行内部访问，这个网络叫ingress网络<pre><code class="hljs routeros"><span class="hljs-comment"># 查看网卡</span>
[root@node1 ~]# docker<span class="hljs-built_in"> network </span>ls
NETWORK ID          NAME                DRIVER              SCOPE
48ceedaeac62       <span class="hljs-built_in"> bridge </span>            <span class="hljs-built_in"> bridge </span>             local
ca5db32546cb        docker_gwbridge    <span class="hljs-built_in"> bridge </span>             local
dc088ea51e19        host                host                local
lu1ybn8aesmn        ingress             overlay             swarm
cec68349a7d5        none                <span class="hljs-literal">null</span>                local
<span class="hljs-comment"># 发现多出来一个 ingress 网卡</span>
<span class="hljs-comment"># 查看详细信息</span>
[root@node1 ~]# docker<span class="hljs-built_in"> network </span>inspect ingress 
            <span class="hljs-string">&quot;Config&quot;</span>: [
                &#123;
                    <span class="hljs-string">&quot;Subnet&quot;</span>: <span class="hljs-string">&quot;10.0.0.0/24&quot;</span>,
                    <span class="hljs-string">&quot;Gateway&quot;</span>: <span class="hljs-string">&quot;10.0.0.1&quot;</span>
                &#125;
            ]
<span class="hljs-comment"># 为10网段</span>
</code></pre></li>
<li>routing mesh可以将外部的请求路由到不同的容器内，从而实现外部网络对service的访问。</li>
<li>service和service之间通讯是因为服务发现</li>
</ul>
<h2 id="service-之间的服务发现"><a href="#service-之间的服务发现" class="headerlink" title="service 之间的服务发现"></a>service 之间的服务发现</h2><p><img src="/images/docker/swarmfwfx.png" srcset="/img/loading.gif"></p>
<ul>
<li><p>相同的容器会放在同一个service 容器易变化（迁移、增加、伸缩）需要知道想连的数据变化</p>
</li>
<li><p>比如</p>
<blockquote>
<p>有3个service nginx mysql php</p>
</blockquote>
<blockquote>
<p>如果数据写入量巨大，就增加mysql的容器数量，那么php就要通过服务发现去发现新增加的这些mysql</p>
</blockquote>
<blockquote>
<p>如果 php中有一个容器坏掉了那么nginx在分配请求时，就需要服务发现，来避免往哪个坏掉的php来发送请求</p>
</blockquote>
</li>
<li><p>ingress 网卡只能做到一个service当中的负载均衡，是不能够服务发现的，所以需要创建overlay网卡来进行服务发现</p>
<blockquote>
<p>创建overlay网卡</p>
</blockquote>
<pre><code class="hljs routeros"><span class="hljs-comment"># 创建网卡</span>
[root@node1 ~]# docker<span class="hljs-built_in"> network </span>create --driver overlay myapp_net
aubwvrdcez7l37vjxund70nul
<span class="hljs-comment"># 查看</span>
[root@node1 ~]# docker<span class="hljs-built_in"> network </span>ls
NETWORK ID          NAME                DRIVER              SCOPE
48ceedaeac62       <span class="hljs-built_in"> bridge </span>            <span class="hljs-built_in"> bridge </span>             local
ca5db32546cb        docker_gwbridge    <span class="hljs-built_in"> bridge </span>             local
dc088ea51e19        host                host                local
lu1ybn8aesmn        ingress             overlay             swarm
aubwvrdcez7l        myapp_net           overlay             swarm
cec68349a7d5        none                <span class="hljs-literal">null</span>                local

<span class="hljs-comment"># worker节点没有同步</span>
[root@node2 ~]# docker<span class="hljs-built_in"> network </span>ls
NETWORK ID          NAME                DRIVER              SCOPE
bbaffe7597dc       <span class="hljs-built_in"> bridge </span>            <span class="hljs-built_in"> bridge </span>             local
a357bf02cc8b        docker_gwbridge    <span class="hljs-built_in"> bridge </span>             local
433526ca900b        host                host                local
lu1ybn8aesmn        ingress             overlay             swarm
595c66e2ae10        none                <span class="hljs-literal">null</span>                local
<span class="hljs-comment"># swarm局限在某一范围内，只在swarm有效</span></code></pre>
<blockquote>
<p>创建 service副本</p>
</blockquote>
<pre><code class="hljs routeros">[root@node1 ~]# docker<span class="hljs-built_in"> service </span>create --name util --network myapp_net busybox sleep 10000000
8kt8b8cbwp562ua0c4y9gjluf
overall progress: 1 out of 1 tasks 
1/1: running   
verify:<span class="hljs-built_in"> Service </span>converged 

<span class="hljs-comment"># 创建3个httpd</span>
[root@node1 ~]# docker<span class="hljs-built_in"> service </span>create --name lmk_web --network myapp_net --replicas 3 httpd
mvd8nmb6vuob31zuo2z5kpbtq
overall progress: 3 out of 3 tasks 
1/3: running   
2/3: running   
3/3: running   
verify:<span class="hljs-built_in"> Service </span>converged 

<span class="hljs-comment"># 都指定的是一个网卡</span></code></pre>
<blockquote>
<p>进入util容器访问三个httpd服务</p>
</blockquote>
<pre><code class="hljs routeros">[root@node1 ~]# docker<span class="hljs-built_in"> service </span>ps util 
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE                <span class="hljs-builtin-name">ERROR</span>               PORTS
lbp4islu0lz0        util.1              busybox:latest      node2               Running             Running about a minute ago  


<span class="hljs-comment"># util ping lmk_web</span>
[root@node2 ~]# docker exec -it util.1.lbp4islu0lz02qjdh7m6k6wt9 sh
/ #<span class="hljs-built_in"> ping </span>lmk_web
PING lmk_web (10.0.1.5): 56 data bytes
64 bytes <span class="hljs-keyword">from</span> 10.0.1.5: <span class="hljs-attribute">seq</span>=0 <span class="hljs-attribute">ttl</span>=64 <span class="hljs-attribute">time</span>=0.111 ms
64 bytes <span class="hljs-keyword">from</span> 10.0.1.5: <span class="hljs-attribute">seq</span>=1 <span class="hljs-attribute">ttl</span>=64 <span class="hljs-attribute">time</span>=0.069 ms
<span class="hljs-comment"># 发现ping的是 10.0.1.5</span>
<span class="hljs-comment"># 那么查看三个 httpd的ip</span>
[root@node2 ~]# docker inspect lmk_web.2.x7753z1m21loqp1iy7314w6b2 
<span class="hljs-string">&quot;IPAddress&quot;</span>: <span class="hljs-string">&quot;10.0.1.7&quot;</span>,

[root@node3 ~]# docker inspect lmk_web.1.id9r3j9ur9ey450gu9exz3azw 
<span class="hljs-string">&quot;IPAddress&quot;</span>: <span class="hljs-string">&quot;10.0.1.6&quot;</span>,

[root@node3 ~]# docker inspect lmk_web.3.tcvk0uoqpkbase90h8xazl5n6 

<span class="hljs-string">&quot;IPAddress&quot;</span>: <span class="hljs-string">&quot;10.0.1.8&quot;</span>,

<span class="hljs-comment"># 发现都不是 10.0.1.5</span></code></pre></li>
<li><p>那么10.0.1.5是什么呢，</p>
<pre><code class="hljs vala"><span class="hljs-meta"># 查看lmk_web这个service</span>
[root@node1 ~]# docker inspect lmk_web 
        <span class="hljs-string">&quot;Endpoint&quot;</span>: &#123;
            <span class="hljs-string">&quot;Spec&quot;</span>: &#123;
                <span class="hljs-string">&quot;Mode&quot;</span>: <span class="hljs-string">&quot;vip&quot;</span>
            &#125;,
            <span class="hljs-string">&quot;VirtualIPs&quot;</span>: [
                &#123;
                    <span class="hljs-string">&quot;NetworkID&quot;</span>: <span class="hljs-string">&quot;aubwvrdcez7l37vjxund70nul&quot;</span>,
                    <span class="hljs-string">&quot;Addr&quot;</span>: <span class="hljs-string">&quot;10.0.1.5/24&quot;</span>
                &#125;
            ]
        &#125;

<span class="hljs-meta"># 会发现10.0.1.5这个ip在这里</span>
<span class="hljs-meta"># 服务发现采用的方式为VIP</span>
将<span class="hljs-number">10.0</span><span class="hljs-number">.1</span><span class="hljs-number">.5</span> 分配为 vip 其他服务访问到这个服务只需要访问这个ip就可以访问到服务了
</code></pre>

</li>
</ul>
<h2 id="swarm回滚更新"><a href="#swarm回滚更新" class="headerlink" title="swarm回滚更新"></a>swarm回滚更新</h2><ul>
<li>生产环境中，某个数据需要向里面部署新的代码，就需要把新的代码打包到新的镜像中，新的镜像就会替代原来容器中的镜像，即为滚动更新，如果代码有bug，就需要回滚。<blockquote>
<p>查看当前 service的版本</p>
</blockquote>
<pre><code class="hljs routeros">[root@node1 ~]# docker<span class="hljs-built_in"> service </span>ps lmk_web 
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE            <span class="hljs-builtin-name">ERROR</span>               PORTS
id9r3j9ur9ey        lmk_web.1           httpd:latest        node3               Running             Running 11 minutes ago                       
x7753z1m21lo        lmk_web.2           httpd:latest        node2               Running             Running 12 minutes ago                       
tcvk0uoqpkba        lmk_web.3           httpd:latest        node3               Running             Running 11 minutes ago   
<span class="hljs-comment"># 为最新版     </span></code></pre>
<blockquote>
<p>更新到 2.4.32版本 （降级也叫更新）</p>
</blockquote>
<pre><code class="hljs routeros">[root@node1 ~]# docker<span class="hljs-built_in"> service </span>update --image httpd:2.4.32 lmk_web 
lmk_web
overall progress: 3 out of 3 tasks 
1/3: running   
2/3: running   
3/3: running   
verify:<span class="hljs-built_in"> Service </span>converged 
[root@node1 ~]# docker<span class="hljs-built_in"> service </span>ps lmk_web 
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE             <span class="hljs-builtin-name">ERROR</span>               PORTS
yeqaksw5qjel        lmk_web.1           httpd:2.4.32        node3               Running             Running 42 seconds ago                        
id9r3j9ur9ey         \_ lmk_web.1       httpd:latest        node3               Shutdown            Shutdown 59 seconds ago                       
smhb2pf9eaia        lmk_web.2           httpd:2.4.32        node2               Running             Running 24 seconds ago                        
x7753z1m21lo         \_ lmk_web.2       httpd:latest        node2               Shutdown            Shutdown 32 seconds ago                       
r7h5rm6h13bz        lmk_web.3           httpd:2.4.32        node3               Running             Running 37 seconds ago                        
tcvk0uoqpkba         \_ lmk_web.3       httpd:latest        node3               Shutdown            Shutdown 37 seconds ago    

<span class="hljs-comment"># 会发现是先停掉 一个在启动一个 这是避免不能对外进行服务</span></code></pre>
<blockquote>
<p>调整副本总量为6个 每次更新2个 1m30s更新一次</p>
</blockquote>
<pre><code class="hljs angelscript"><span class="hljs-string">[root@node1 ~]</span># docker service update --replicas <span class="hljs-number">6</span> --update-parallelism <span class="hljs-number">2</span> --update-delay <span class="hljs-number">1</span>m30s lmk_web 
lmk_web
overall progress: <span class="hljs-number">6</span> <span class="hljs-keyword">out</span> of <span class="hljs-number">6</span> tasks 
<span class="hljs-number">1</span>/<span class="hljs-number">6</span>: running   
<span class="hljs-number">2</span>/<span class="hljs-number">6</span>: running   
<span class="hljs-number">3</span>/<span class="hljs-number">6</span>: running   
<span class="hljs-number">4</span>/<span class="hljs-number">6</span>: running   
<span class="hljs-number">5</span>/<span class="hljs-number">6</span>: running   
<span class="hljs-number">6</span>/<span class="hljs-number">6</span>: running   
verify: Service converged 

# 查看
<span class="hljs-string">[root@node1 ~]</span># docker service ps lmk_web 
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE                ERROR               PORTS
yeqaksw5qjel        lmk_web<span class="hljs-number">.1</span>           httpd:<span class="hljs-number">2.4</span><span class="hljs-number">.32</span>        node3               Running             Running <span class="hljs-number">3</span> minutes ago                            
id9r3j9ur9ey         \_ lmk_web<span class="hljs-number">.1</span>       httpd:latest        node3               Shutdown            Shutdown <span class="hljs-number">3</span> minutes ago                           
smhb2pf9eaia        lmk_web<span class="hljs-number">.2</span>           httpd:<span class="hljs-number">2.4</span><span class="hljs-number">.32</span>        node2               Running             Running <span class="hljs-number">3</span> minutes ago                            
x7753z1m21lo         \_ lmk_web<span class="hljs-number">.2</span>       httpd:latest        node2               Shutdown            Shutdown <span class="hljs-number">3</span> minutes ago                           
r7h5rm6h13bz        lmk_web<span class="hljs-number">.3</span>           httpd:<span class="hljs-number">2.4</span><span class="hljs-number">.32</span>        node3               Running             Running <span class="hljs-number">3</span> minutes ago                            
tcvk0uoqpkba         \_ lmk_web<span class="hljs-number">.3</span>       httpd:latest        node3               Shutdown            Shutdown <span class="hljs-number">3</span> minutes ago                           
<span class="hljs-number">5</span>dz0cc4j4e8r        lmk_web<span class="hljs-number">.4</span>           httpd:<span class="hljs-number">2.4</span><span class="hljs-number">.32</span>        node2               Running             Running about a minute ago                       
yxzjha6hbao6        lmk_web<span class="hljs-number">.5</span>           httpd:<span class="hljs-number">2.4</span><span class="hljs-number">.32</span>        node2               Running             Running about a minute ago                       
uv9db0moba6a        lmk_web<span class="hljs-number">.6</span>           httpd:<span class="hljs-number">2.4</span><span class="hljs-number">.32</span>        node3               Running             Running about a minute ago</code></pre></li>
<li>回滚——docker swarm 只能回滚到上一次执行docker service update之前的状态，并不能无限回滚</li>
<li>只能回滚最后两次的版本</li>
</ul>
<h2 id="swarm-数据迁移"><a href="#swarm-数据迁移" class="headerlink" title="swarm 数据迁移"></a>swarm 数据迁移</h2><ul>
<li>tasks</li>
</ul>
<h2 id="swarm-数据映射"><a href="#swarm-数据映射" class="headerlink" title="swarm 数据映射"></a>swarm 数据映射</h2><h2 id="swarm-健康检查"><a href="#swarm-健康检查" class="headerlink" title="swarm 健康检查"></a>swarm 健康检查</h2>
            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/docker/">docker</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/docker/">docker</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">本博客所有文章是以学习为目的，如果有不对的地方可以一起交流沟通共同学习 邮箱:1248287831@qq.com！</p>
              
              
                <div class="post-prevnext row">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2020/12/21/docker-k8s%E5%88%9B%E5%BB%BA%E5%89%AF%E6%9C%AC/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">docker-k8s-创建副本</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2020/12/15/docker-ELK%E6%97%A5%E5%BF%97%E9%87%87%E9%9B%86/">
                        <span class="hidden-mobile">docker-ELK日志采集</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div id="tocbot"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    
  </main>

  
    <a id="scroll-top-button" href="#" role="button">
      <i class="iconfont icon-arrowup" aria-hidden="true"></i>
    </a>
  

  
    <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
  

  

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://space.bilibili.com/39165374" target="_blank" rel="nofollow noopener"><span>LMK</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  
  <div class="statistics">
    
    

    
      
        <!-- 不蒜子统计PV -->
        <span id="busuanzi_container_site_pv" style="display: none">
            总访问量 
            <span id="busuanzi_value_site_pv"></span>
             次
          </span>
      
      
        <!-- 不蒜子统计UV -->
        <span id="busuanzi_container_site_uv" style="display: none">
            总访客数 
            <span id="busuanzi_value_site_uv"></span>
             人
          </span>
      
    
  </div>


  

  
</footer>

<!-- SCRIPTS -->
<script  src="https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js" ></script>
<script  src="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/js/bootstrap.min.js" ></script>
<script  src="/js/debouncer.js" ></script>
<script  src="/js/main.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/lazyload.js" ></script>
  



  



  <script defer src="https://cdn.staticfile.org/clipboard.js/2.0.6/clipboard.min.js" ></script>
  <script  src="/js/clipboard-use.js" ></script>



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





  <script  src="https://cdn.staticfile.org/tocbot/4.11.1/tocbot.min.js" ></script>
  <script>
    $(document).ready(function () {
      var boardCtn = $('#board-ctn');
      var boardTop = boardCtn.offset().top;

      tocbot.init({
        tocSelector: '#tocbot',
        contentSelector: '#post-body',
        headingSelector: 'h1,h2,h3,h4,h5,h6',
        linkClass: 'tocbot-link',
        activeLinkClass: 'tocbot-active-link',
        listClass: 'tocbot-list',
        isCollapsedClass: 'tocbot-is-collapsed',
        collapsibleClass: 'tocbot-is-collapsible',
        collapseDepth: 0,
        scrollSmooth: true,
        headingsOffset: -boardTop
      });
      if ($('.toc-list-item').length > 0) {
        $('#toc').css('visibility', 'visible');
      }
    });
  </script>



  <script  src="https://cdn.staticfile.org/typed.js/2.0.11/typed.min.js" ></script>
  <script>
    var typed = new Typed('#subtitle', {
      strings: [
        '  ',
        "docker-swarm集群&nbsp;",
      ],
      cursorChar: "_",
      typeSpeed: 70,
      loop: false,
    });
    typed.stop();
    $(document).ready(function () {
      $(".typed-cursor").addClass("h2");
      typed.start();
    });
  </script>



  <script  src="https://cdn.staticfile.org/anchor-js/4.2.2/anchor.min.js" ></script>
  <script>
    anchors.options = {
      placement: "right",
      visible: "hover",
      
    };
    var el = "h1,h2,h3,h4,h5,h6".split(",");
    var res = [];
    for (item of el) {
      res.push(".markdown-body > " + item)
    }
    anchors.add(res.join(", "))
  </script>



  <script  src="/js/local-search.js" ></script>
  <script>
    var path = "/local-search.xml";
    var inputArea = document.querySelector("#local-search-input");
    inputArea.onclick = function () {
      searchFunc(path, 'local-search-input', 'local-search-result');
      this.onclick = null
    }
  </script>



  <script  src="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.css" />

  <script>
    $('#post img:not(.no-zoom img, img[no-zoom]), img[zoom]').each(
      function () {
        var element = document.createElement('a');
        $(element).attr('data-fancybox', 'images');
        $(element).attr('href', $(this).attr('src'));
        $(this).wrap(element);
      }
    );
  </script>















</body>
</html>
